<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .done {
            text-decoration: line-through;
            color: #999;
        }
    </style>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<body>
    <div id="app">
        <input type="text" v-model="newMes">
        <input type="button" value="添加" @click="add">

        <p>代表列表</p>
        <ul>
            <li v-for="todo of todoList" :class="todo.done ? 'done' : '' ">
                <input type="checkbox" :checked="todo.done" @click.prevent="change(todo.id)">
                {{ todo.mes }}
                <a href="javascript:;" @click="del(todo.id)">删除</a>
            </li>
        </ul>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                todoList: [],
                newMes: ''
            },
            created() {
                this.getTodo()
            },
            methods: {
                getTodo() {
                    fetch('/todolist').then(res => {
                        return res.json()
                    }).then(rsp => {
                        this.todoList = rsp
                    })
                },

                add() {
                    fetch('/add', {
                        method: 'post',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ mes: this.newMes })
                    }).then(res => {
                        return res.json()
                    }).then(rsp => {
                        if (!rsp.code) {
                            this.todoList.push(rsp.data)
                        } else {
                            alert(rsp.mes)
                        }
                        this.newMes = ''
                    })
                },

                del(id) {
                    fetch('/del', {
                        method: 'post',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ id })
                    }).then(res => {
                        return res.json()
                    }).then(rsp => {
                        if (rsp.code === 0) {
                            return this.getTodo()
                        }
                        alert(rsp.mes)
                    })
                },

                change(id) {
                    fetch('change', {
                        method: 'post',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify({ id })
                    }).then(res => {
                        return res.json()
                    }).then(rsp => {
                       this.todoList = rsp.data
                    })
                }
            }
        })
    </script>
</body>

</html>